<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

$keyname = $DB->escape(get('keyname'));
$curVersion = $DB->escape(get('current_version'));

$packageid = (int) $DB->oneValue($CONFIG->dbprefix.'vms_packages', 'id', "keyname = '{$keyname}'");

if($curVersion !== '0.0.0' && $DB->countRows($CONFIG->dbprefix.'vms_versions', "version = '{$curVersion}' AND package_id = '{$packageid}'")==0){
    // could not find the version
    $res->result = 'FAIL';
    $res->reason = 'Could not find the current version "'.$curVersion.'"';
}else{
    // find the next version
    $vData = $DB->fetchRow("SELECT * FROM
        {$CONFIG->dbprefix}vms_versions v JOIN {$CONFIG->dbprefix}vms_versions_to_groups v2g ON (v.id = v2g.version_id)
        JOIN {$CONFIG->dbprefix}iusers_to_groups i2g ON (v2g.group_key = i2g.group_key)
        JOIN {$CONFIG->dbprefix}iusers i ON (i2g.user_id = i.id)
        WHERE v.package_id = '{$packageid}' AND v.previous_version = '{$curVersion}' AND i.id = '1' ORDER BY version DESC LIMIT 1");

    

    if(!is_array($vData) || count($vData)==0){
        $res->result = 'SUCCESS';
        $res->version = $curVersion;
    }else{
        $res->result = 'SUCCESS';
        $res->version = $vData['version'];
    }
}

die(serialize($res));




